package de.dhbw.ka.tit09.picsim.model;

import de.dhbw.ka.tit09.picsim.BitUtility;

/**
 * Model for the PIC timers.
 * 
 * @author Andreas Bender
 * 
 */
public class PICTimers extends AbstractModelObject {
	private PICModelProvider pICModelProvider;
	private int powerUpTimer;

	// private int watchdogTimer;

	//
	// public void setWatchdogTimer(int watchdogTimer) {
	// this.watchdogTimer = watchdogTimer;
	// }
	//
	// public int getWatchdogTimer() {
	// return watchdogTimer;
	// }

	/**
	 * Sets the power up timer.
	 * 
	 * @param powerUpTimer the new timer value
	 */
	public void setPowerUpTimer(int powerUpTimer) {
		firePropertyChange("powerUpTimer", powerUpTimer,
				this.powerUpTimer = powerUpTimer);

	}

	/**
	 * Returns the value of the power up timer.
	 * 
	 * @return the power up timer value
	 */
	public int getPowerUpTimer() {
		return powerUpTimer; // *400ns?
	}

	/**
	 * Counts a program cycle. Modifies timers.
	 */
	public void countCycle() {
		firePropertyChange("powerUpTimer", powerUpTimer, ++this.powerUpTimer);

		// watchdogTimer--;

		// if internal timer mode (T0CS clear), inc TMR0
		if (!BitUtility.getBitValueFromInt(pICModelProvider.getSFRegisters()
				.getOPTION(), 5)) {
			pICModelProvider.getSFRegisters().incTMR0();
		}
	}

	/**
	 * Load timers with default values.
	 */
	public void reset() {
		pICModelProvider = PICModelProvider.getInstance();
		setPowerUpTimer(0);
		// setWatchdogTimer(30);// FIXME
	}
}
